package com.sgb.jicai.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.qgb.zcore.event.WebEvent;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.databinding.ActivityJicaiQuotedPriceBinding;
import com.sgb.jicai.entity.OfferGroupBuyingDetailsEntity;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.view.fragment.JiCaiGuidanceFragment;
import com.sgb.jicai.view.fragment.JiCaiQuotedPriceJoinInFragment;
import com.sgb.jicai.view.fragment.OfferRequireFragment;
import com.sgb.jicai.view.fragment.QuotedPriceOfferFragment;
import com.sgb.jicai.viewmodel.JcChanceListModel;
import com.sgb.jicai.viewmodel.JiCaiQuotedPriceModel;
import com.sgb.link.entity.EventConstant;
import com.sgb.link.utils.UnitListUtils;
import com.swgk.core.base.AppActivity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.TimeUtil;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.swgk.core.view.areapopup.AddressEntity;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 参与报价   ---集采详情后参与报价页面
 * 修改报价    复用
 */
public class JiCaiQuotedPriceActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    JiCaiQuotedPriceModel model;
    ActivityJicaiQuotedPriceBinding binding;
    private String groupbuyingNo;
    public static final String KEY_ID = "jicai_id";
    private String jId;
    public static final String KEY_JILIU_id = "jiaoliu_id";
    // 1.参与报价   2.修改报价
    private int type;
    public static final String KEY_TYPE = "jicai_quoted_type";
    private OfferGroupBuyingDetailsEntity detailEntity;
    //地区
    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    //    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> filtrationAddressEntityList;//按省份过滤后的数据
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private String provinceNo;//省级编码
    private String provinceName;//省级名称
    private String cityNo;//城市编码
    private String cityName;//城市名称
    private String districtNo;//区编码
    private String districtName;//区名称

    //集采类型(1是即报即采, 2是汇集采购, 3是企业专项)
    private String groupBuyType;
    public static final String GROUP_BIY_TYPE = "jiCaiGroupBuyType";
    private QuotedPriceOfferFragment quotedPriceOfferFragment;
    private String url = "";

    public static void start(Context context, String id, String jId, int type, String groupBuyType,String url) {
        Intent intent = new Intent();
        intent.putExtra(KEY_ID, id);
        intent.putExtra(KEY_JILIU_id, jId);
        intent.putExtra(KEY_TYPE, type);
        intent.putExtra(GROUP_BIY_TYPE, groupBuyType);
        intent.putExtra("url", url);
        intent.setClass(context, JiCaiQuotedPriceActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(JiCaiQuotedPriceActivity.this, R.layout.activity_jicai_quoted_price);
        model = new ViewModelProvider(this).get(JiCaiQuotedPriceModel.class);
        model.setActivity(JiCaiQuotedPriceActivity.this);
        url = getIntent().getStringExtra("url");
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.llInterflow.setOnClickListener(this::onMultiClick);
        binding.tvSubmit.setOnClickListener(this::onMultiClick);
        binding.area.setOnWholeItemClickListener(this);
    }

    /* v1.13改版为多tab */
    private void updateTabFra() {
        String[] mTitles = new String[]{"参与报价", "报价规则", "集采规则", "报价指导", "加盟列表"};
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mFragments.add(quotedPriceOfferFragment);
        mFragments.add(OfferRequireFragment.newInstance(groupbuyingNo, binding.viewpager, 1));
        mFragments.add(OfferRequireFragment.newInstance(groupbuyingNo, binding.viewpager, 2));
        mFragments.add(JiCaiGuidanceFragment.newInstance(groupbuyingNo, binding.viewpager, 3));
        mFragments.add(JiCaiQuotedPriceJoinInFragment.newInstance(groupbuyingNo, binding.viewpager, 4));
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewpager.setOffscreenPageLimit(mTitles.length);
        binding.viewpager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.setTabMode(mTitles.length > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
        binding.viewpager.resetHeight(0);
        binding.tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                binding.viewpager.resetHeight(pos);
                binding.viewpager.setCurrentItem(pos);
                if (pos == 0) {
                    binding.tvHint.setVisibility(View.VISIBLE);
                    binding.viewDivision.setVisibility(View.GONE);
                } else {
                    binding.tvHint.setVisibility(View.GONE);
                    binding.viewDivision.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void init() {
        super.init();
        groupbuyingNo = getIntent().getStringExtra(KEY_ID);
        jId = getIntent().getStringExtra(KEY_JILIU_id);
        type = getIntent().getIntExtra(KEY_TYPE, 1);
        groupBuyType = getIntent().getStringExtra(GROUP_BIY_TYPE);
        if (type == 2) {
            binding.tvTitle.setText("修改报价");
            model.getDetailForOffer(groupbuyingNo, 0);//0编辑 1新增
        } else {
            binding.tvTitle.setText("参与报价");
            model.getDetailForOffer(groupbuyingNo, 1);//0编辑 1新增
        }
        quotedPriceOfferFragment = QuotedPriceOfferFragment.newInstance(binding.viewpager, 0, groupBuyType, type, this);
        model.getAddressModel();//请求省市区数据
        updateTabFra();
    }

    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            if (!TextUtils.isEmpty(url)){
                finish();
                JiCaiBaseApi.startWeb(this,url);
            }else {
                finish();
            }
        } else if (id == R.id.tv_submit) {
            submitDatas();
        } else if (id == R.id.ll_interflow) {
            if (TextUtils.isEmpty(jId)) {
                MToast.showToast(this, "未设置客服人员，不支持交流");
            } else {
                model.findCustomerServiceImAccount(jId);
            }
        }
    }

    public void updateOtherView(OfferGroupBuyingDetailsEntity detailsEntity) {
        this.detailEntity = detailsEntity;
        if (type == 2) {//修改报价
            jId = detailEntity.getCustomerServiceContactsId();
            //将值填写到各view
            provinceNo = detailEntity.getProvinceNo();//省级编码
            provinceName = detailEntity.getProvinceName();//省级名称
            cityNo = detailEntity.getCityNo();//城市编码
            cityName = detailEntity.getCityName();//城市名称
            districtNo = detailEntity.getDistrictNo();//区编码
            districtName = detailEntity.getDistrictName();//区名称
            binding.area.setContent(provinceName + "/" + cityName + "/" + districtName);//省市区
            binding.etDetailsAddress.setEditContent(detailEntity.getDetailsAddress());
            binding.principalman.setContent(detailEntity.getDirector());
            binding.principalTel.setText(detailEntity.getDirectorNumber());
            binding.linkman.setContent(detailEntity.getContacts());
            //   binding.linkman.setEnable(false);
            binding.linkmanTel.setText(detailEntity.getContactNumber());
            // binding.linkmanTel.setFocusable(false);
            // binding.linkmanTel.setFocusableInTouchMode(false);
      /*      if ("2".equals(detailsEntity.getGroupBuyType())) {
                for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails groupBuyingDetails : detailEntity.getGroupBuyingDetails()) {
                    if (null != groupBuyingDetails && groupBuyingDetails.getDetails() != null) {
                        for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails details : groupBuyingDetails.getDetails()) {
                            groupBuyingDetails.setSupplyTime(details.getSupplyTime());
                            groupBuyingDetails.setPriceRule(details.getPriceRule());
                        }
                    }
                }
            }*/
            if (2 == type) {
                for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails groupBuyingDetails : detailEntity.getGroupBuyingDetails()) {
                    if (null != groupBuyingDetails && groupBuyingDetails.getDetails() != null) {
                        for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails details : groupBuyingDetails.getDetails()) {
                            details.setType(1);
                          /*  if (TextUtils.isEmpty(details.getSupplyTime())) {
                                details.setType(1);
                            } else {
                                details.setType(2);
                            }*/
                        }
                    }
                }
            }
        } else {//参与报价
            String name = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, "");
            if (!TextUtils.isEmpty(name)) {
                binding.linkman.setContent(name);
                //binding.linkman.setEnable(false);
            }
            String tel = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
            if (!TextUtils.isEmpty(tel)) {
                binding.linkmanTel.setText(tel);
                // binding.linkmanTel.setFocusable(false);
                //  binding.linkmanTel.setFocusableInTouchMode(false);
            }
        }
        quotedPriceOfferFragment.setDetailEntity(detailsEntity);
    }

    public void submitDatas() {
        if (null == detailEntity) {
            return;
        }
        if (TextUtils.isEmpty(provinceNo)) {
            MToast.showToast(this, "请选择省市区信息");
            return;
        }
        if (TextUtils.isEmpty(binding.etDetailsAddress.getEditContent())) {
            MToast.showToast(this, "请填写详细地址");
            return;
        }
        if (TextUtils.isEmpty(binding.linkman.getContent())) {
            MToast.showToast(this, "请填写联系人");
            return;
        }
        if (TextUtils.isEmpty(binding.linkmanTel.getText().toString().trim())) {
            MToast.showToast(this, "请填写联系人电话");
            return;
        }
        if (!CheckUtils.isMobileNum(binding.linkmanTel.getText().toString().trim())) {
            MToast.showToast(this, "联系人手机号码填写错误");
            return;
        }
        if (TextUtils.isEmpty(binding.principalman.getContent())) {
            MToast.showToast(this, "请填写责任人");
            return;
        }
        if (TextUtils.isEmpty(binding.principalTel.getText().toString().trim())) {
            MToast.showToast(this, "请填写责任人电话");
            return;
        }
        if (!TextUtils.isEmpty(binding.principalTel.getText().toString().trim())) {
            if (!CheckUtils.isMobileNum(binding.principalTel.getText().toString().trim())) {
                MToast.showToast(this, "责任人手机号码填写错误");
                return;
            }
        }
        //1.13改版后报价列表信息从fragment中获取并设置
        detailEntity.setGroupBuyingDetails(quotedPriceOfferFragment.getDetailEntity().getGroupBuyingDetails());
        detailEntity.setOfferNotes(quotedPriceOfferFragment.getDetailEntity().getOfferNotes());

       /* if ("1".equals(groupBuyType)) { //既报既采非空逻辑处理
            if (seaveGroupBuyType1()) return;
        } else { //会及采购逻辑处理
            if (seaveGroupBuyType2()) return;
        }*/
        if (seaveGroupBuyType1()) return;

        detailEntity.setProvinceNo(provinceNo);
        detailEntity.setProvinceName(provinceName);
        detailEntity.setCityNo(cityNo);
        detailEntity.setCityName(cityName);
        detailEntity.setDistrictNo(districtNo);
        detailEntity.setDistrictName(districtName);
        detailEntity.setDetailsAddress(binding.etDetailsAddress.getEditContent());
        detailEntity.setContacts(binding.linkman.getContent());
        detailEntity.setContactNumber(binding.linkmanTel.getText().toString().trim());
        detailEntity.setDirector(binding.principalman.getContent());
        detailEntity.setDirectorNumber(binding.principalTel.getText().toString().trim());

         /*   if ("1".equals(groupBuyType)) { //既报既采数据提交前重新构造
            if (detailEntity.getRecords() != null){
                detailEntity.getRecords().clear();
            }
            if (type!=2){ // 首次报价
                detailEntity.setRecords(setJbjcData());
            }else { // 修改报价
                detailEntity.setGroupBuyingDetails(setJbjcData());
            }
        } else { //汇集采购数据重新封装处理
            detailEntity.setRecords(detailEntity.getGroupBuyingDetails());
        }
     if ("1".equals(groupBuyType)) {
            waringPop(detailEntity,null);
        } else {
            model.priceOfferWarning(detailEntity);
        }*/

        if (detailEntity.getRecords() != null) {
            detailEntity.getRecords().clear();
        }
        if (null != detailEntity.getGroupBuyingDetails() && detailEntity.getGroupBuyingDetails().size() > 0) {
            for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails groupBuyingDetail : detailEntity.getGroupBuyingDetails()) {
                if (null != groupBuyingDetail.getDetails() && groupBuyingDetail.getDetails().size() > 0) {
                    for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails detail : groupBuyingDetail.getDetails()) {
                        if (!TextUtils.isEmpty(detail.getStock())) {
                            if (Double.valueOf(detail.getStock()) <= 0) {
                                MToast.showToast(this, "供应量必须大于0");
                                return;
                            }
                            if (!TextUtils.isEmpty(groupBuyingDetail.getUnitName()) && !UnitListUtils.getUnitStart(groupBuyingDetail.getUnitName())) {
                                if (detail.getStock().indexOf(".") > 0) {
                                    MToast.showToast(this, "当前单位供应量为整数，请重新输入");
                                    return;
                                }
                            }
                        }
                        if (!TextUtils.isEmpty(detail.getPrice())) {
                            if (Double.valueOf(detail.getPrice()) <= 0) {
                                MToast.showToast(this, "我的报价必须大于0");
                                return;
                            }

                        }
                    }
                }
            }
        }
        List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> groupBuyingDetails = setJbjcData();
        if (groupBuyingDetails.size() > 0) {
            detailEntity.setRecords(groupBuyingDetails);
            waringPop(detailEntity, null);
        } else {
            MToast.showToast(this, "请填写完整信息");
        }

    }

    /**
     * 对既报既采提交数据再次封装
     */
    private List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> setJbjcData() {
        if (null != detailEntity.getGroupBuyingDetails() && detailEntity.getGroupBuyingDetails().size() > 0) {
            //创建新数据集合
            List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> list = new ArrayList<>();
            // 循环外部商品数据提交内容
            for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails groupBuyingDetails : detailEntity.getGroupBuyingDetails()) {
                if (null != groupBuyingDetails.getDetails() && groupBuyingDetails.getDetails().size() > 0) {
                        //将每条商品中的每个报价信息循环取出来放在和最外层数据并列
                        for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails records : groupBuyingDetails.getDetails()) {
                            if (!TextUtils.isEmpty(records.getSupplyTime()) && !TextUtils.isEmpty(records.getPrice()) && !TextUtils.isEmpty(records.getSupplyTimeStart()) && !TextUtils.isEmpty(records.getStock())) {
                                OfferGroupBuyingDetailsEntity.GroupBuyingDetails buyingDetails = new OfferGroupBuyingDetailsEntity.GroupBuyingDetails();
                                buyingDetails.setAttrs(groupBuyingDetails.getAttrs());
                                buyingDetails.setGoodsDescribe(groupBuyingDetails.getGoodsDescribe());
                                buyingDetails.setGoodsName(groupBuyingDetails.getGoodsName());
                                buyingDetails.setGroupbuyingDetailNo(groupBuyingDetails.getGroupbuyingDetailNo());
                                buyingDetails.setJoinNum(groupBuyingDetails.getJoinNum());
                                buyingDetails.setPriceRule(groupBuyingDetails.getPriceRule());
                                buyingDetails.setRangeBrands(groupBuyingDetails.getRangeBrands());
                                buyingDetails.setUnitName(groupBuyingDetails.getUnitName());
                                buyingDetails.setPrice(records.getPrice() + "");//单价
                                try {
                                    buyingDetails.setSupplyTime(String.valueOf(TimeUtil.getLongTime(records.getSupplyTime(), "yyyy-MM-dd")));
                                    buyingDetails.setSupplyTimeStart(String.valueOf(TimeUtil.getLongTime(records.getSupplyTimeStart(), "yyyy-MM-dd")));
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                buyingDetails.setStock(records.getStock());
                                buyingDetails.setSort(String.valueOf(list.size()));
                                list.add(buyingDetails);
                            } else {
                                //一个商品单条报价数据和多条数据处理
                                if (groupBuyingDetails.getDetails().size()>1){
                                    list.clear();//在用户新增加时一条信息有值其他必须有值
                                    return list;
                                }
                            }
                        }
                    }
            }
            return list;
        }
        return new ArrayList<>();
    }

    /**
     * 汇集采购处理逻辑
     *
     * @return
     */
    private boolean seaveGroupBuyType2() {
        //判断至少有一条加盟数据
        boolean hasQuotedData = false;
        List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> groupBuyingDetailes = detailEntity.getGroupBuyingDetails();
        for (int i = 0; i < groupBuyingDetailes.size(); i++) {
            OfferGroupBuyingDetailsEntity.GroupBuyingDetails gbDetail = groupBuyingDetailes.get(i);
            if ((null != gbDetail.getPriceRule() && gbDetail.getPriceRule().size() > 0) && !TextUtils.isEmpty(gbDetail.getSupplyTime()) && !TextUtils.isEmpty(gbDetail.getSupplyTimeStart())) {
                hasQuotedData = true;
                break;
            }
        }
        if (!hasQuotedData) {
            MToast.showToast(this, "请至少完整填写一条报价信息");
            return true;
        }
        return false;
    }

    /**
     * 即报即采 数据处理逻辑
     *
     * @return
     */
    private boolean seaveGroupBuyType1() {
        //判断至少有一条加盟数据
        boolean hasQuotedData = false;
        List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> groupBuyingDetailes = detailEntity.getGroupBuyingDetails();
        for (int i = 0; i < groupBuyingDetailes.size(); i++) {
            OfferGroupBuyingDetailsEntity.GroupBuyingDetails gbDetail = groupBuyingDetailes.get(i);
            List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> priceRuleList = gbDetail.getDetails();
            if (null != priceRuleList && priceRuleList.size() > 0) {
                for (OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule : gbDetail.getDetails()) {
                    if (!TextUtils.isEmpty(priceRule.getSupplyTimeStart()) && !TextUtils.isEmpty(priceRule.getSupplyTime()) && !TextUtils.isEmpty(priceRule.getPrice()) && !TextUtils.isEmpty(priceRule.getStock())) {
                        hasQuotedData = true;
                        break;
                    }
                }
            }
        }
        if (!hasQuotedData) {
            MToast.showToast(this, "请至少完整填写一条报价信息");
            return true;
        }
        return false;
    }

    public void waringPop(OfferGroupBuyingDetailsEntity entity, List waringList) {
        if (null == waringList || waringList.size() == 0) {
           /* if (type == 2) {//修改报价
                model.updateOffer(entity);
            } else {//参与报价
                model.sponsorOffer(entity);
            }*/
            model.sponsorOffer(entity);
        } else {
            StringBuffer detailNo = new StringBuffer();
            //比对结果并提示用户
            for (int i = 0; i < waringList.size(); i++) {
                for (int j = 0; j < detailEntity.getGroupBuyingDetails().size(); j++) {
                    if (waringList.get(i).equals(detailEntity.getGroupBuyingDetails().get(j).getGroupbuyingDetailNo())) {
                        detailNo.append(j + 1).append("、");
                    }
                }
            }

            String numWords = detailNo.toString();
            if (numWords.endsWith("、")) {
                numWords = numWords.substring(0, numWords.length() - 1);
            }
            String msg = "商品第 " + numWords + " 项集采价格大于市场价，是否继续报价？";
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("提示")
                    .setMsg(msg)
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                          /*  if (type == 2) {//修改报价
                                model.updateOffer(entity);
                            } else {//参与报价
                                model.sponsorOffer(entity);
                            }*/
                            model.sponsorOffer(entity);
                        }

                        @Override
                        public void onLeftClick() {

                        }
                    }).show();
        }
    }

    //提交信息成功
    public void submitSucceed() {
        EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_WEBVIEW_RELOAD, ""));
        hideKeyboard();
        WebEvent webEvent = new WebEvent();
        webEvent.setMessageTag(EventConstant.EVENT_WEBVIEW_RELOAD);
        EventBus.getDefault().post(webEvent);
        if (!TextUtils.isEmpty(url)){
            finish();
            JiCaiBaseApi.startWeb(this,url);
        }else {
            finish();
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        if (view.getId() == R.id.area) {//所在地址
            if (addressEntityList != null) {
                showCityPop("请选择地区", 1);
            } else {
                MToast.showToast(this, "获取地区信息出错");
                model.getAddressModel();//请求省市区数据
            }
        }
    }

    //返回省市区数据加载数据
    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        addressEntityList = data;
        initJsonData();
    }

    //组装省市区数据信息
    private void initJsonData() {
        for (int i = 0; i < addressEntityList.size(); i++) {
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {
                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
    }

    //    选择地址弹窗
    private void showCityPop(String title, int num) {
        CityPickView cityOption = new CityPickBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String areaName = "";
                if (opt3.get(options1).get(options2).get(options3) == null) {
                    areaName = "";
                } else {
                    areaName = opt3.get(options1).get(options2).get(options3).getName();
                    cityPosition3 = options3;
                }
                String content = addressEntityList.get(options1).getName() + " / " + opt2.get(options1).get(options2).getName() + " / " + areaName;
                cityPosition1 = options1;
                cityPosition2 = options2;
                provinceNo = addressEntityList.get(options1).getId();
                provinceName = addressEntityList.get(options1).getName();
                if (opt2.get(options1).get(options2) != null) {
                    cityNo = opt2.get(options1).get(options2).getNo();
                    cityName = opt2.get(options1).get(options2).getName();
                }
                if (opt3.get(options1).get(options2).get(options3) != null) {
                    districtNo = opt3.get(options1).get(options2).get(options3).getNo();
                    districtName = opt3.get(options1).get(options2).get(options3).getName();
                }
                binding.area.setContent(content);
            }
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText(title)
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    @Override
    public void finish() {
        super.finish();
        hideKeyboard();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //点击完返回键，执行的动作
            submitSucceed();
        }
        return true;
    }

    public void setPictureSelector() {
        model.pictureSelector(1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                String compressPath = selectList.get(0).getRealPath();
                if (!compressPath.contains("http:")) {
                    if (compressPath.contains(".jpg") || compressPath.contains(".jpeg") || compressPath.contains(".png")) {
                        File file = new File(compressPath);
                        model.uploadAnnexFileModel(file, 0);
                        quotedPriceOfferFragment.mPicAdapter.setPicData(selectList);
                    } else {
                        MToast.showLongToast(this, "仅支持上传png/jpeg/jpg格式的图片");
                    }
                }
            }
        }
    }

    // 上传图片回调
    public void uploadResult(String filepath) {
        Log.i("uploadAvatarResult", filepath);
        // detailEntity.setFile(filepath);
        int i = filepath.lastIndexOf("/");
        String name = filepath.substring(i + 1, filepath.length());
        OfferGroupBuyingDetailsEntity.FilesBean fileBean = new OfferGroupBuyingDetailsEntity.FilesBean();
        fileBean.setName(name);
        fileBean.setUrl(filepath);
        List<OfferGroupBuyingDetailsEntity.FilesBean> list1 = new ArrayList<>();
        list1.add(fileBean);
        String file = new Gson().toJson(list1);
        detailEntity.setFile(file);
    }
}
